package top.byfree.jbyan.saber.dao

import cn.dev33.satoken.stp.StpUtil
import org.ktorm.dsl.*
import org.springframework.stereotype.Component
import top.byfree.jbyan.saber.bean.IS_DELETE_NO
import top.byfree.jbyan.saber.bean.Menus
import top.byfree.jbyan.saber.bean.ROOT_PARENT_ID
import top.byfree.jbyan.saber.bean.Users
import top.byfree.jbyan.saber.bean.dto.FilterMenuDto
import top.byfree.jbyan.saber.bean.pojo.Menu
import top.byfree.jbyan.saber.bean.vo.menuVoCol
import top.byfree.jbyan.saber.common.BaseDao
import top.byfree.jbyan.saber.common.coverOrderBy

/**
 * 菜单数据处理层
 *
 * @author jbyan
 * @since 2024/5/15
 */
@Component
class MenuDao : BaseDao<Menu, Menus>(Menus) {

    /** 条件查询菜单列表 */
    fun filter(filterMenuDto: FilterMenuDto): Query {
        val (menuName, perms, isFrame, menuType, menuTypes, visible, status, parentId, gmtCreateStart, gmtCreateEnd, createBy, modifyBy, orderBy, exportCol) = filterMenuDto
        val query = database.from(Menus)
            .select()
            .whereWithConditions { cl ->
                if (!menuName.isNullOrBlank()) {
                    cl += Menus.menuName like "%$menuName%"
                }
                if (!perms.isNullOrBlank()) {
                    cl += Menus.perms like "%$perms%"
                }
                isFrame?.let {
                    cl += Menus.isFrame eq isFrame
                }
                if (!menuType.isNullOrBlank()) {
                    cl += Menus.menuType eq menuType
                }
                if (!parentId.isNullOrBlank()) {
                    cl += Menus.parentId eq parentId
                }
                if (!menuTypes.isNullOrEmpty()) {
                    cl += Menus.menuType inList menuTypes
                }
                visible?.let {
                    cl += Menus.visible eq it
                }
                status?.let {
                    cl += Menus.status eq status
                }
                gmtCreateStart?.let {
                    cl += Menus.gmtCreate gte it
                }
                gmtCreateEnd?.let {
                    cl += Menus.gmtCreate lte it
                }
                if (!createBy.isNullOrBlank()) {
                    cl += Menus.createBy like "%$createBy%"
                }
                if (!modifyBy.isNullOrBlank()) {
                    cl += Menus.modifyBy like "%$modifyBy%"
                }

                if (menuName.isNullOrBlank()
                    && perms.isNullOrBlank()
                    && isFrame == null
                    && menuType.isNullOrBlank()
                    && visible == null
                    && status == null
                    && parentId == null
                    && gmtCreateEnd == null
                    && gmtCreateStart == null
                    && createBy.isNullOrBlank()
                    && modifyBy.isNullOrBlank()
                ) {
                    cl += Menus.parentId eq ROOT_PARENT_ID
                }
            }
        val coverOrderBy = Users.coverOrderBy(orderBy)
        // 添加排序
        return coverOrderBy?.let {
            query.orderBy(coverOrderBy)
        } ?: query
    }


    /* 获取当前拥有权限的菜单信息 */
    fun currentMenus(): List<Menu> {
        // 当前用户拥有的所有权限
        val permissionList = StpUtil.getPermissionList()
            .map { it.replace("*", "") }
            .filter { it.isNotBlank() }

        if (permissionList.isEmpty()) {
            return emptyList()
        }

        // 查询当前用户拥有权限的菜单信息
        return database.from(Menus)
            .select(menuVoCol)
            .where { (Menus.isDelete eq IS_DELETE_NO) and (Menus.perms inList permissionList) }
            .map(Menus::createEntity)
    }


}